﻿using Dapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.DreamTour.Core;
using ZhouYu.DreamTour.DAL.IRepository;
using ZhouYu.DreamTour.Model;

namespace ZhouYu.DreamTour.DAL.Repository
{
  public class OrderRepository: IOrderRepository
    {
        //查询订单状态
        public List<OrderInfo> Orderstatus(int status, int UserId)
        {
            //查询总条数
            string sql = $"SELECT * FROM orderinfo WHERE Order_Status={status} and User_Id = {UserId} ORDER BY Order_Id DESC ";
            List<OrderInfo> orderInfo = DapperHelper<OrderInfo>.QueryList(sql, null);
            return orderInfo;
        }

        /// <summary>
        /// 获取用户订单类型集合
        /// </summary>
        /// <param name="Order_Name">订单类型</param>
        /// <returns></returns>
        public List<OrderInfo> GetOrder(int id)
        {
            //创建OrderInfo对象参数化
            OrderInfo order = new OrderInfo();
            order.User_Id = id;
            //sql语句
            string sql = "select * from OrderInfo where User_Id=@User_Id ORDER BY Order_Id DESC";
            //调用查询方法 
            return DapperHelper<OrderInfo>.QueryList(sql, order);
        }

        /// <summary>
        /// 修改商品为到货状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public int UpdateArrival(int id)
        {
            //创建OrderInfo对象参数化
            OrderInfo order = new OrderInfo();
            order.Order_Id = id;
            //获取当前到货时间
            order.Order_OutingTime = DateTime.Now;
            string sql = @" UPDATE OrderInfo SET Order_Status=1,Order_OutingTime=@Order_OutingTime
            WHERE Order_Id = @Order_Id";
            int result = DapperHelper<OrderInfo>.Execute(sql, order);
            return result;
        }
        /// <summary>
        /// 修改商品为已评价
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Alreadyevaluated(int id)
        {
            //创建OrderInfo对象参数化
            OrderInfo order = new OrderInfo();
            order.Order_Id = id;
            //获取当前到货时间
            order.Order_OutingTime = DateTime.Now;
            string sql = @" UPDATE OrderInfo SET Order_Status=4,Order_OutingTime=@Order_OutingTime
            WHERE Order_Id = @Order_Id";
            int result = DapperHelper<OrderInfo>.Execute(sql, order);
            return result;
        }
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int CancelSpecialty(int id)
        {
            //创建OrderInfo对象参数化
            OrderInfo order = new OrderInfo();
            order.Order_Id = id;
            string sql = "DELETE  FROM OrderInfo WHERE  Order_Id = @Order_Id";
            int result = DapperHelper<OrderInfo>.Execute(sql, order);
            return result;
        }

        //添加商品评价
        public int AddCommodityevaluate(Usercomment usercomment)
        {
            //获取当前时间
            usercomment.UserComment_CreateTime = DateTime.Now;
            string sql = @"INSERT INTO usercomment(User_Id,UserComment_Type,UserComment_PruId,UserComment_GoodBad,UserComment_CreateTime,UserComment_Content,UserComment_Score)
            VALUES (@User_Id,@UserComment_Type,@UserComment_PruId,@UserComment_GoodBad,@UserComment_CreateTime,@UserComment_Content,@UserComment_Score)";
            int result = DapperHelper<Usercomment>.Execute(sql, usercomment);
            return result;
        }

        #region 添加订单信息
        /// <summary>
        /// 添加订单信息
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <returns></returns>
        public bool AddOrderInfo(OrderInfo orderInfo)
        {
            string sql = @"INSERT INTO `orderinfo`(`Order_No`,`User_Id`,`Order_ProInfo`,`Order_Type`,`Order_Money`,`Order_ReserveTime`,`Order_OutingTime`,`Order_Status`,`Order_Name`,`Orde_PictureUrl`,`Order_Content`) VALUES(@Order_No,@User_Id,@Order_ProInfo,@Order_Type,@Order_Money,@Order_ReserveTime,@Order_OutingTime,@Order_Status,@Order_Name,@Orde_PictureUrl,@Order_Content);";
            return DapperHelper<OrderInfo>.Execute(sql, orderInfo)>0;
        }
        #endregion

        #region 根据订单号查询订单信息
        /// <summary>
        /// 根据订单号查询订单信息
        /// </summary>
        /// <param name="Order_No"></param>
        /// <returns></returns>
        public OrderInfo SelOrderInfoByNo(string Order_No)
        {
            string sql = @"SELECT * FROM OrderInfo WHERE `Order_No`=@Order_No";
            //DynamicParameters dynamicParameters = new DynamicParameters();
            //dynamicParameters.Add("@Order_No", Order_No);
            return DapperHelper<OrderInfo>.QuerySingleOrDefault(sql, new {Order_No= Order_No });
        }
        #endregion

        #region 修改订单状态
        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="Order_No"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool UpdateOrderStatus(string Order_No,int Order_Status)
        {
            string sql = @"UPDATE orderinfo SET `Order_Status`=@Order_Status WHERE Order_No=@Order_No and Order_Status=0";
            return DapperHelper<OrderInfo>.Execute(sql, new { Order_Status = Order_Status, Order_No = Order_No })>0;
        }
        #endregion
    }
}
